/*
 * $Id: macros.h 91 2009-04-02 07:06:32Z hyunghwan.chung $
 *
   Copyright 2005-2009 Chung, Hyung-Hwan.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an  BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

#ifndef _XP_MACROS_H_
#define _XP_MACROS_H_

#include <xp/types.h>

#ifdef __cplusplus
#	if XP_SIZEOF_VOID_P == XP_SIZEOF_INT
#		define XP_NULL (0)
#	elif XP_SIZEOF_VOID_P == XP_SIZEOF_LONG
#		define XP_NULL (0l)
#	elif XP_SIZEOF_VOID_P == XP_SIZEOF_LONG_LONG
#		define XP_NULL (0ll)
#	else
#		define XP_NULL (0)
#	endif
#else
#	define XP_NULL ((void*)0)
#endif

#define XP_CHAR_EOF  ((xp_cint_t)-1)

#define XP_SIZEOF(n)   (sizeof(n))
#define XP_COUNTOF(n)  (sizeof(n) / sizeof(n[0]))
#define XP_OFFSETOF(type,member) ((xp_size_t)&((type*)0)->member)
#define XP_ALIGNOF(type) XP_OFFSETOF(struct { char d1; type d2 }, d2)

#define xp_sizeof(n)   (sizeof(n))
#define xp_countof(n)  (sizeof(n) / sizeof(n[0]))
#define xp_offsetof(type,member) ((xp_size_t)&((type*)0)->member)
#define xp_alignof(type) xp_alignof(struct { char d1; type d2 }, d2)

#if defined(_WIN32) && defined(XP_CHAR_IS_WCHAR) && !defined(__LCC__)
#	define xp_main wmain
#elif defined(XP_CHAR_IS_MCHAR)
#	define xp_main main
#endif

#define XP_TYPE_IS_SIGNED(type) (((type)0) > ((type)-1))
#define XP_TYPE_IS_UNSIGNED(type) (((type)0) < ((type)-1))
#define XP_TYPE_MAX(type) \
	((XP_TYPE_IS_SIGNED(type)? (type)~((type)1 << (XP_SIZEOF(type) * 8 - 1)): (type)(~(type)0)))
#define XP_TYPE_MIN(type) \
	((XP_TYPE_IS_SIGNED(type)? (type)((type)1 << (XP_SIZEOF(type) * 8 - 1)): (type)0))

#define XP_NUM_IS_POWOF2(x) (((x) & ((x) - 1)) == 0)
#define XP_SWAP(x,y,original_type,casting_type) \
	do { \
		x = (original_type)((casting_type)(x) ^ (casting_type)(y)); \
		y = (original_type)((casting_type)(y) ^ (casting_type)(x)); \
		x = (original_type)((casting_type)(x) ^ (casting_type)(y)); \
	} while (0)
#define XP_ABS(x) ((x) < 0? -(x): (x))

#define XP_LOOP_CONTINUE(id) goto __loop_ ## id ## _begin__;
#define XP_LOOP_BREAK(id)    goto __loop_ ## id ## _end__;
#define XP_LOOP_BEGIN(id)    __loop_ ## id ## _begin__: {
#define XP_LOOP_END(id)      XP_LOOP_CONTINUE(id) } __loop_ ## id ## _end__:;

#define XP_REPEAT(n,blk) \
	do { \
		xp_size_t __xp_repeat_x1 = (xp_size_t)(n); \
		xp_size_t __xp_repeat_x2 = __xp_repeat_x1 >> 4; \
		__xp_repeat_x1 &= 15; \
		while (__xp_repeat_x1-- > 0) { blk; } \
		while (__xp_repeat_x2-- > 0) { \
			blk; blk; blk; blk; blk; blk; blk; blk; \
			blk; blk; blk; blk; blk; blk; blk; blk; \
		} \
	} while (0);

/* deprecated */
#define XP_MQUOTE_I(val)  #val
#define XP_MQUOTE(val)    XP_MQUOTE_I(val)
#define XP_MCHAR(ch)      ((xp_mchar_t)ch)
#define XP_MTEXT(txt)     ((const xp_mchar_t*)txt)

/* new short form */
#define XP_MQ_I(val) #val
#define XP_MQ(val)   XP_MQ_I(val)
#define XP_MC(ch)    ((xp_mchar_t)ch)
#define XP_MS(str)   ((const xp_mchar_t*)str)
#define XP_MT(txt)   (txt)

/* TODO: if the compiler doesn't have the built-in wchar_t support
 *       XP_WCHAR & XP_WTEXT must be defined differently.
#define XP_WCHAR(ch) ((xp_wchar_t)ch)
#define XP_WTEXT(txt) don't know yet... may have to call a function?
 */

/* deprecated */
#define XP_WQUOTE_I(val) (L ## #val)
#define XP_WQUOTE(val)   XP_WQUOTE_I(val)
#define XP_WCHAR(ch)     ((xp_wchar_t)L ## ch)
#define XP_WTEXT(txt)    ((const xp_wchar_t*)L ## txt)

/* new short form */
#define XP_WQ_I(val)  (L ## #val)
#define XP_WQ(val)    XP_WQ_I(val)
#define XP_WC(ch)     ((xp_wchar_t)L ## ch)
#define XP_WS(str)    ((const xp_wchar_t*)L ## str)
#define XP_WT(txt)    (L ## txt)

#if defined(XP_CHAR_IS_MCHAR)
	/* deprecated */
#	define XP_CHAR(ch)   XP_MCHAR(ch)
#	define XP_TEXT(txt)  XP_MTEXT(txt)
#	define XP_QUOTE(val) XP_MQUOTE(val)

	/* new short form */
#	define XP_C(ch)  XP_MC(ch)
#	define XP_S(str) XP_MS(str)
#	define XP_T(txt) XP_MT(txt)
#	define XP_Q(val) XP_MQ(val)
#else
	/* deprecated */
#	define XP_CHAR(ch)   XP_WCHAR(ch)
#	define XP_TEXT(txt)  XP_WTEXT(txt)
#	define XP_QUOTE(val) XP_WQUOTE(val)

	/* new short form */
#	define XP_C(ch)  XP_WC(ch)
#	define XP_S(str) XP_WS(str)
#	define XP_T(txt) XP_WT(txt)
#	define XP_Q(val) XP_WQ(val)
#endif

#ifdef __cplusplus
#	define XP_BEGIN_NAMESPACE(x)    namespace x {
#	define XP_END_NAMESPACE(x)      }
#	define XP_BEGIN_NAMESPACE2(x,y) namespace x { namespace y {
#	define XP_END_NAMESPACE2(y,x)   }}
#endif

#endif
